#pragma once

#include "../EntityManager.h"
#include "../Utility.h"
#include "../Market/Comodities.h"

#include "Platform.h"

class InputManager
{
private:
	// An instance of Storage Data.
	// Every instance that correpsonds to the same entity must be adjacent in memory.
	struct InstanceData
	{
		uint32 size;
		uint32 capacity;
		void* buffer;

		Entity* entity;				// The entity owning this instance.
		float* storage;
		float* max_storage;
		float* rate;				// The maximum amount consumed or produced per production cycle.
		float* ratio;				// The ratio of the rate to the available invintory. Note that by definition this can't be higher than one.
		uint32* comodity_index;
		uint32* max_range;
		uint32* min_range;
	};

private:
	InstanceData _data;

	void Allocate(const uint32 capacity);
	void SortData(const uint32 start, const uint32 stop);

public:
	InputManager();
	~InputManager();

	void CalculateInputRatio();
	void FindMinProduction(const uint32 output_count, const Entity* const entities, float* ratios);
	void OverrideRatio(const uint32 entity_count, const Entity* const entities, float* ratios);
	void ConsumeInput();

	void PlaceMarketBuys(BidData* bids) const;
	void RegisterBuyResults(const BidData* const bids, const float* const comodity_mean, const float* const comodity_supply, const float* const comodity_demand, const uint32 num_resources);
	void RemoveInputData(const OpenVector<Entity>& defunct_entities, OpenVector<uint32>& defunct_buyers);
	uint32 AllocateStorage(const Entity* const entities, const uint32 num_entities, const uint32* const comodity_indexes, const float* const comodity_rates, const uint32 num_inputs,
		const float* const mean_pricesd);
};